@isTest
private class CodeCoverageUtil
{

    static testMethod void testMethod1() 
    {
        test.startTest();
        Template__c tmp = new Template__c(name = 'TTT');
        insert tmp;
        Template__c[] tempT = [select name from Template__c where id = :tmp.id];
        system.assertEquals(tmp.name, tempT[0].name);

        Attachment a = new Attachment(name = 'Test Attach.doc', Body = Blob.valueOf('<html><body>This is {Attachment}</body></html>'), parentId = tmp.Id);
        insert a;
        //Id of the Attachment which is not used to create Plan Docs
        Attachment[] aT = [select Id from Attachment a where a.ParentId = :tmp.id];
        if(aT.size() > 0)
            delete aT;

        
        Template__c tName = new Template__c(Name = 'Sample');
        insert tName;
        tempT = [select name from Template__c where id = :tName.id];
        system.assertEquals(tName.name, tempT[0].name);

        Template__c[] t = [select Id from Template__c t where t.Id = :tmp.id];
        if(t.size() > 0)
        {
            delete t;
        }
        Sales_Contract__c sc1 = new Sales_Contract__c();
        insert sc1;
        Sales_Contract__c[] scT = [select id from Sales_Contract__c where id = :sc1.id];
        system.assertEquals(sc1.id, scT[0].id);
        
        Plan_Document_Inquiry__c pd1 = new Plan_Document_Inquiry__c(Plan_Document__c = sc1.id, InquiryStatus__c = 'Pending');
        insert pd1;
        Plan_Document_Inquiry__c[] pdT = [select InquiryStatus__c from Plan_Document_Inquiry__c where id = :pd1.id];
        system.assertEquals(pd1.InquiryStatus__c, pdT[0].InquiryStatus__c);
        
        pdT[0].InquiryStatus__c = 'Inquiry Approved';
        update pdT;
        
        Test.stopTest();
    }


    static testMethod void testMethod2() 
    {
        test.startTest();
        Sales_Contract__c sc1 = new Sales_Contract__c();
        insert sc1;
        Sales_Contract__c[] scT = [select id from Sales_Contract__c where id = :sc1.id];
        system.assertEquals(sc1.id, scT[0].id);

        Plan_Document_Inquiry__c pd1 = new Plan_Document_Inquiry__c(Plan_Document__c = sc1.id, InquiryStatus__c = 'Pending');
        insert pd1;
        Plan_Document_Inquiry__c[] pdT = [select InquiryStatus__c from Plan_Document_Inquiry__c where id = :pd1.id];
        system.assertEquals(pd1.InquiryStatus__c, pdT[0].InquiryStatus__c);
        pdT[0].InquiryStatus__c = 'Inquiry Rejected';
        update pdT;

        Test.stopTest();
    }

    static testMethod void testMethod3() 
    {
        test.startTest();

        CallidusDataSource__c cds = new CallidusDataSource__c();
        insert cds;
        Attachment a = new Attachment(name = 'Test Attach3.txt', Body = Blob.valueOf('<html><body>This is {Attachment}</body></html>'), parentId = cds.Id);
        insert a;
        CallidusDataSource__c[] cdsList = [select Id, In_Use_Attachments__c from CallidusDataSource__c where id = :cds.id];
        system.assertEquals(cds.id, cdsList[0].id);
        cdsList[0].In_Use_Attachments__c = a.id;
        update cdsList;
        try
        {
            delete a;
        }
        catch(Exception e){}
        
        
        Remote_IP_Address__c[] rpList = [select id, Is_Role_Shown__c from Remote_IP_Address__c];
        delete rpList;
        Remote_IP_Address__c[] rpList2 = [select id, Is_Role_Shown__c from Remote_IP_Address__c];
        system.assertEquals(rpList2.size() ,0);
        
        Callidus_CommonUtilG.savePlanSearchConfigurations('yes', null, null, null, null);
        Callidus_CommonUtilG.savePlanSearchConfigurations('no', null, null, null, null);
        Test.stopTest();
    }

    static testMethod void testMethod4() 
    {
        try
        {
            Sales_Contract__c sc = new Sales_Contract__c(Status__c = 'Draft', UserId__c = UserInfo.getUserID(), Level1__c = UserInfo.getUserId());
            insert sc;
            Sales_Contract__c sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
            system.assertEquals(sctest.Status__c, sc.Status__c);
            sc.Status__c = 'Distributed';
            update sc;
            sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
            system.assertEquals(sctest.Status__c, sc.Status__c);
            Callidus_CommonUtilG.getPlantype();
            Callidus_CommonUtilG.getPlanAcceptMessage(sc.Id);
            Approval.ProcessSubmitRequest request =new Approval.ProcessSubmitRequest();
            request.setObjectId(sc.Id);
            Approval.ProcessResult result = Approval.process(request);
            
            ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId = :sc.id and Status = 'Pending'];
            ProcessInstanceWorkitem[] prInstWItem;
            if(prInst.size() > 0)
            {
                prInstWItem = [select Id, OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id];
            }
            PageReference pg = Page.Callidus_Plan_Approval_By_Manager;
            pg.getParameters().put('id', sc.id);
            pg.getParameters().put('pid', prInstWitem[0].id);
            
            Test.setCurrentPage(pg);
            
            Callidus_PlanApprovalRejectionByManager cpar = new Callidus_PlanApprovalRejectionByManager();
            cpar.comments = 'Test';
            cpar.approve();
        }
        catch(exception e){}            
    }

    static testMethod void testMethod5() 
    {
        try
        {
            Sales_Contract__c sc = new Sales_Contract__c(Status__c = 'Draft', UserId__c = UserInfo.getUserID(), Level1__c = UserInfo.getUserId());
            insert sc;
            Sales_Contract__c sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
            system.assertEquals(sctest.Status__c, sc.Status__c);
            sc.Status__c = 'Distributed';
            update sc;
            sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
            system.assertEquals(sctest.Status__c, sc.Status__c);
    
            Approval.ProcessSubmitRequest request =new Approval.ProcessSubmitRequest();
            request.setObjectId(sc.Id);
            Approval.ProcessResult result = Approval.process(request);
            
            ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId = :sc.id and Status = 'Pending'];
            ProcessInstanceWorkitem[] prInstWItem;
            if(prInst.size() > 0)
            {
                prInstWItem = [select Id, OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id];
            }
            PageReference pg = Page.Callidus_Plan_Approval_By_Manager;
            pg.getParameters().put('id', sc.id);
            pg.getParameters().put('pid', prInstWitem[0].id);
            
            Test.setCurrentPage(pg);
            
            Callidus_PlanApprovalRejectionByManager cpar = new Callidus_PlanApprovalRejectionByManager();
            cpar.comments = 'Test';
            cpar.reject();
        }
        catch(Exception e){}        
    }


    static testMethod void testMethod6() 
    {
        try
        {
            Sales_Contract__c sc = new Sales_Contract__c(Status__c = 'Draft', UserId__c = UserInfo.getUserID(), Level1__c = UserInfo.getUserId());
            insert sc;
            Sales_Contract__c sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
            system.assertEquals(sctest.Status__c, sc.Status__c);
            sc.Status__c = 'Distributed';
            update sc;
            sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
            system.assertEquals(sctest.Status__c, sc.Status__c);
    
            Approval.ProcessSubmitRequest request =new Approval.ProcessSubmitRequest();
            request.setObjectId(sc.Id);
            Approval.ProcessResult result = Approval.process(request);
            
            ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId = :sc.id and Status = 'Pending'];
            ProcessInstanceWorkitem[] prInstWItem;
            if(prInst.size() > 0)
            {
                prInstWItem = [select Id, OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id];
            }
            PageReference pg = Page.Callidus_ReassignApprovalRequestVF;
            pg.getParameters().put('id', sc.id);
            pg.getParameters().put('pid', prInstWitem[0].id);
            
            Test.setCurrentPage(pg);
            
            Callidus_ReassignApprovalRequest crar = new Callidus_ReassignApprovalRequest();
            crar.pdi.User__c = Userinfo.getUserId();
            crar.reassign();
        }
        catch(Exception e){}        
    }

    static testMethod void testCommonUtil() 
    {
        List<Task> tList = new List<Task>();
        Callidus_CommonUtil ccu = new Callidus_CommonUtil();
        Task t = ccu.newTask(null, UserInfo.getUserId(), null, 'Synchronizing user data completed successfully.', 'Synchronizing user data completed successfully.', Date.today(), DateTime.now(), true, 'High', 'Finished', 'Priority');
        tList.add(t);
        String message = ccu.insertTasks(tList);
        
        System.assertEquals('success', message);
        
        ccu.insertTasks(null);
        
        EmailTemplate emailTemplate = [select Id,Name,Subject,body from EmailTemplate where name like :'Callidus_Synchronize_User_Data_Mail'];
                                                  
        Messaging.SingleEmailMessage[] mailList = new Messaging.SingleEmailMessage[]{};
                            
        mailList.add(ccu.createNewEmail(null, UserInfo.getUserId(), emailTemplate.Id, null, false, false));
        
    }

    public static testMethod void testBatch() 
    {
        try
        {
            User[] uList = new User[]{};
            for(integer i = 0; i<20; i++)
            {
                User u = new User(Lastname = 'testBatchApexUser', Username = 'enduser'+i+'@gmail.com', Email = 'enduser'+i+'@gmail.com', Alias = 'enduser', CommunityNickname = 'euser'+i, TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'ISO-8859-1', ProfileId = UserInfo.getProfileId(), LanguageLocaleKey = 'en_US', ManagerId = UserInfo.getUserId());
                uList.add(u);
            }
            insert uList;
            
            Test.StartTest();
            Callidus_DataSourceBatchUtil cdsbu = new Callidus_DataSourceBatchUtil();
            cdsbu.query1 += ' Limit 20 ';
            ID batchprocessid = Database.executeBatch(cdsbu);
            
            cdsbu = new Callidus_DataSourceBatchUtil();
            cdsbu.query1 += ' Limit 20 ';
            batchprocessid = Database.executeBatch(cdsbu);
            Test.StopTest();
            
            String endUser = 'testBatchApexUser';
            System.AssertEquals(database.countquery('SELECT count()'+' FROM User WHERE Lastname = :endUser'), 20);
        }
        catch(Exception e){}
    }

    public static testMethod void testBatch2() 
    {
        try
        {
            User[] uList = new User[]{};
            for(integer i = 0; i<20; i++)
            {
                User u = new User(Firstname = 'Test', Lastname = 'testBatchApexUser', Username = 'enduser'+i+'@gmail.com', Email = 'enduser'+i+'@gmail.com', Alias = 'enduser', CommunityNickname = 'euser'+i, TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'ISO-8859-1', ProfileId = UserInfo.getProfileId(), LanguageLocaleKey = 'en_US', ManagerId = UserInfo.getUserId());
                uList.add(u);
            }
            insert uList;
            
            Test.StartTest();
            Callidus_DataSourceBatchUtil cdsbu = new Callidus_DataSourceBatchUtil();
            cdsbu.query1 += ' Limit 20 ';
            ID batchprocessid = Database.executeBatch(cdsbu);
            
            cdsbu = new Callidus_DataSourceBatchUtil();
            cdsbu.query1 += ' Limit 20 ';
            batchprocessid = Database.executeBatch(cdsbu);
            Test.StopTest();
            
            String endUser = 'testBatchApexUser';
            System.AssertEquals(database.countquery('SELECT count()'+' FROM User WHERE Lastname = :endUser'), 20);
        }
        catch(Exception e){}
    }
    
    static testMethod void dacuTestCase() 
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject sObj = new CallidusDataSOurce__c();
        Map<String, Schema.Sobjectfield> fieldsMap = dacu.getFieldMap(sObj);
        String[] expectedFieldMap = new String[]{'Id','Name'};
        Set<String> allowedFields = dacu.getAccessibleFields(sObj, fieldsMap, expectedFieldMap);
        Set<String> filteredList = dacu.getFilteredCreateableFields(fieldsMap, allowedFields);
        filteredList = dacu.getFilteredUpdateableFields(fieldsMap, allowedFields);
        system.assertEquals('success', dacu.isCreateableObjectByUser(sObj));
        system.assertEquals('success', dacu.isUpdateableObjectByUser(sObj));
        system.assertEquals('success', dacu.isDeleteableObjectByUser(sObj));
        dacu.pageMessage(sobj, 'create');
        
    }
    static testMethod void testGetTemplateAttachmentsVF() 
    {
        try
        {
            PageReference pg = Page.DoNotAccept;
            
            Test.setCurrentPage(pg);
            
            Template__c t = new Template__c(name = 'Test12321');
            insert t;
            Template__c[] tempT = [select name from Template__c where id = :t.id];
            system.assertEquals(t.name, tempT[0].name);
            
            try
            {
                Template__c tDup = new Template__c(name = 'Test12321');
                insert tDup;
            }
            catch(Exception e){}
    
            Attachment a = new Attachment(name = 'Test Attach.doc', Body = Blob.valueOf('<html><body>This is {Attachment}</body></html>'), parentId = t.Id);
            insert a;
            Attachment[] attT = [select name, Body, Parentid from Attachment where id = :a.id];
            system.assertEquals(a.name, attT[0].name);
            system.assertEquals(a.Body.toString(), attT[0].Body.toString());
            system.assertEquals(a.ParentId, attT[0].ParentId);
            
            Attachment a1 = new Attachment(name = 'Test', Body = Blob.valueOf('<html><body>This is {Attachment}</body></html>'), parentId = t.Id);
            insert a1;
            attT = [select name, Body, Parentid from Attachment where id = :a.id];
            system.assertEquals(a.name, attT[0].name);
            system.assertEquals(a.Body.toString(), attT[0].Body.toString());
            system.assertEquals(a.ParentId, attT[0].ParentId);
            
            Sales_Contract__c sc = new Sales_Contract__c(TemplateName__c = t.name, UserID__c = UserInfo.getUserId(), AttachmentID__c = a.id, AttachmentIdVals__c = a.Id, Number_of_Attachments__c = 1, Status__c = 'Viewed', Email_Notification_1__c = UserInfo.getUserId(), Email_Notification_2__c = UserInfo.getUserId(), Email_Notification_3__c = UserInfo.getUserId());
            insert sc;
            Sales_Contract__c[] scT = [select id from Sales_Contract__c where id = :sc.Id];
            system.assertEquals(sc.id, scT[0].id);
            pg.getParameters().put('sId', sc.id);       
            Callidus_DoNotAcceptVF gtvf = new Callidus_DoNotAcceptVF();
            gtvf.reason = '';
            gtvf.saveReason();
            gtvf = new Callidus_DoNotAcceptVF();
            gtvf.reason = 'Not Accepted';
            gtvf.saveReason();
            gtvf.updateStatusToRejected();
            
        }
        catch(Exception e){}
    }

    static testMethod void testPlanManageCS() 
    {
        try
        {
            PageReference pg = Page.Callidus_Plan_ManageCS_VF;
            Test.setCurrentPage(pg);
            CallidusPlanManageCS cp = new CallidusPlanManageCS();
            cp.sc.User_ID__c = UserInfo.getUserID();
            cp.getDetails();
            cp.save();
            cp.reset();
        }
        catch(Exception e){}
    }

    static testMethod void testCCWS() 
    {
        CCWS.getPlanNamearr('','','');
        
        CCWS.getPlanNameMap(null, '');
    }

    static testMethod void testCase() {

        Template__c[] tempT = null;
        Batch__c[] batchT = null;
        BatchedUser__c[] buT = null;
        Sales_Contract__c[] scT = null;
        Approvers__c[] appT = null;
        Template_Role__c[] trT = null;
        
        Template__c t = new Template__c(name = 'generateTitle', EligibilityType__c = 'Title');
        
        Test.startTest();
        
        insert t;
        tempT = [select name from Template__c where id = :t.id];
        system.assertEquals(t.name, tempT[0].name);
    
        Batch__c btch = new Batch__c(Name='generateTest', Effective_Start_Date__c = Date.today(), Effective_End_Date__c = Date.today(), Status__c = 'Completed', StartTime__c = System.now(), EndTime__c = System.now(), TotalContracts__c = 5, RemainingContracts__c = 3, TemplateId__c = t.Id);
        insert btch;
        batchT = [select id from Batch__c where id = :btch.id];
        system.assertEquals(btch.id, batchT[0].id);

        BatchedUser__c bu = new BatchedUser__c(Batchid__c = btch.Id);
        insert bu;
        buT = [select Batchid__c from BatchedUser__c where id = :bu.id];
        system.assertEquals(bu.Batchid__c, buT[0].Batchid__c);
            
        Approvers__c app = new Approvers__c(Template__c = t.Id, Admin__c = UserInfo.getUserId());
        insert app;
        appT = [select Template__c, Admin__c from Approvers__c where id=:app.id];
        system.assertEquals(app.Template__c, appT[0].Template__c);
        system.assertEquals(app.Admin__c, appT[0].Admin__c);

        Template_Role__c tr = new Template_Role__c(Template_ID__c = t.Id, User_Title__c = 'Manager');
        insert tr;
        trT = [select Template_ID__c  from Template_Role__c where id = :tr.id];
        system.assertEquals(tr.Template_ID__c, trT[0].Template_ID__c);
        
        Attachment att = new Attachment(name = 'generateAttach', body = Blob.valueOf('<html><body>This is example for {TemplateAttachment}.</body></html>'), parentId = t.Id);
        insert att;
        Attachment[] attT = [select name from Attachment where id = :att.id];
        system.assertEquals(att.name, attT[0].name);
        
        String[] attachList = new String[]{att.id, att.name};
        System.assertEquals(GenerateUtil.getTemplateAttachments(t.Id), attachList);
        Test.stopTest();
        
    }
    
    static testMethod void testCase6() 
    {
        
        try
        {       
            Test.startTest();
            CallidusDataSource__c cds = new CallidusDataSource__c(User_Title__c = 'Manager');
            insert cds;
            Attachment att1 = new Attachment(name = 'generateAttach1', body = Blob.valueOf('<html><body>This is example for {TemplateAttachment}.</body></html>'), parentId = cds.Id);
            insert att1;
            
            CallidusDataSource__c[] cdsT = [select id from CallidusDataSource__c where id=:cds.id];
            system.assertEquals(cds.id, cdsT[0].id);
            
            
            Profile[] p = [select id from Profile where name = 'End User'];
            User u = null;
            u = new User(Lastname = 'enduser', Username = 'enduser1@gmail.com', Email = 'enduser@gmail.com', Alias = 'enduser', CommunityNickname = 'euser', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'ISO-8859-1', ProfileId = p[0].Id, LanguageLocaleKey = 'en_US', ManagerId = UserInfo.getUserId());
            insert u;
            Sales_Contract__c sc = new Sales_Contract__c(UserId__c = u.id, Can_Accept_Plan__c = true, Level1__c = u.id, Level2__c = u.id, Level3__c = u.id, Level4__c = u.id, Level5__c = u.id, Level6__c = u.id, Email_Notification_1__c = u.id, Email_Notification_2__c = u.id, Email_Notification_3__c = u.id, AttachmentIDVals__c = att1.id);
            insert sc;
            
            GenerateUtil.populateNameValuePairs(sc, cds);
            
            Sales_Contract__c[] scRet = [select id, name from Sales_Contract__c where id = :sc.id];
            system.assertEquals(sc.id, scRet[0].id);
            
            String scName = scRet[0].name;
            GenerateUtil.testVar = 'Published';
            Callidus_CommonUtilG.updateStatus(scName);
            GenerateUtil.testVar = 'Plan Approved 1';
            Callidus_CommonUtilG.updateStatus(scName);

            Callidus_CommonUtilG.savePlanDocumentName('2011 Plan Document');
            Remote_IP_Address__c[] rip = [select id from Remote_IP_Address__c];
            delete rip;
            Callidus_CommonUtilG.savePlanDocumentName('2011 Plan Document');
            
            GenerateUtil.testVar = 'Plan Rejected 1';
           // Callidus_CommonUtilG.updateStatus(scName);
            HandlingAttachments.updateStatusForNonE(scName);
    
            Test.stopTest();
        
        }
        catch(Exception e)
        {}
    }

    static testMethod void testCase1() 
    {
        try
        {
            
            
            Test.startTest();
            UserRole[] ur = [select id from UserRole limit 1];
            CallidusDataSource__c cds = new CallidusDataSource__c(UserRoleId__c = ur[0].Id);
            insert cds;
            CallidusDataSource__c[] cdsT = [select id from CallidusDataSource__c where id=:cds.id];
            system.assertEquals(cds.id, cdsT[0].id);
            
            Template__c t1 = new Template__c(name = 'generateRole', EligibilityType__c = 'Role');
            insert t1;
            Template__c[] tempT = [select name from Template__c where id = :t1.id];
            system.assertEquals(t1.name, tempT[0].name);
            
            Batch__c btch1 = new Batch__c(Name='generateTest2', Effective_Start_Date__c = Date.today(), Effective_End_Date__c = Date.today(), Status__c = 'Completed', StartTime__c = System.now(), EndTime__c = System.now(), TotalContracts__c = 5, RemainingContracts__c = 3, TemplateId__c = t1.Id);
            insert btch1;
            Batch__c[] batchT = [select id from Batch__c where id = :btch1.id];
            system.assertEquals(btch1.id, batchT[0].id);
    
            BatchedUser__c bu = new BatchedUser__c(Batchid__c = btch1.Id);
            insert bu;
            BatchedUser__c[] buT = [select Batchid__c from BatchedUser__c where id = :bu.id];
            system.assertEquals(bu.Batchid__c, buT[0].Batchid__c);
        
            Approvers__c app1 = new Approvers__c(Template__c = t1.Id, Admin__c = UserInfo.getUserId());
            insert app1;
            Approvers__c[] appT = [select Template__c, Admin__c from Approvers__c where id=:app1.id];
            system.assertEquals(app1.Template__c, appT[0].Template__c);
            system.assertEquals(app1.Admin__c, appT[0].Admin__c);
    
            Template_Role__c tr1 = new Template_Role__c(Template_ID__c = t1.Id, Role_ID__c = ur[0].id);
            insert tr1;
            Template_Role__c[] trT = [select Template_ID__c  from Template_Role__c where id = :tr1.id];
            system.assertEquals(tr1.Template_ID__c, trT[0].Template_ID__c);
            
            Attachment att1 = new Attachment(name = 'generateAttach', body = Blob.valueOf('<html><body>This is example for {TemplateAttachment}.</body></html>'), parentId = t1.Id);
            insert att1;
            Attachment[] attT = [select name from Attachment where id = :att1.id];
            if(attT.size() > 0)
                system.assertEquals(att1.name, attT[0].name);
            
            GenerateUtil.generateForUsers2(String.valueOf(t1.Id), String.valueOf(btch1.id), '2009-10-10', 'Local', String.valueOf(att1.Id), '2009-10-11', '2009-10-12');
            
            Test.stopTest();
        }
        catch(Exception e){}
    }

    static testMethod void testCase2() 
    {
        try
        {
            Template__c[] tempT = null;
            Batch__c[] batchT = null;
            BatchedUser__c[] buT = null;
            Sales_Contract__c[] scT = null;
            Approvers__c[] appT = null;
            
            Template__c t = new Template__c();
            Batch__c b = new Batch__c();
    
            Test.startTest();
            System.assertEquals(GenerateUtil.sendMailToUser(String.valueOf(UserInfo.getUserId()) ,'test','test','test'), 'success');
            insert b;
            batchT = [select id from Batch__c where id = :b.id];
            system.assertEquals(b.id, batchT[0].id);
            
            Sales_Contract__c sc = new Sales_Contract__c(BatchID__c = b.id);
            insert sc;
            scT = [select BatchId__c from Sales_Contract__c where id = :sc.id];
            system.assertEquals(sc.BatchID__c, scT[0].BatchID__c);
            insert t;
            
            tempT = [select id from Template__c where id = :t.id];
            system.assertEquals(t.id, tempT[0].id);
            
            Approvers__c app = new Approvers__c(Template__c = t.Id, Admin__c = UserInfo.getUserId());
            insert app;
            appT = [select Template__c, Admin__c from Approvers__c where id=:app.id];
            system.assertEquals(app.Template__c, appT[0].Template__c);
            system.assertEquals(app.Admin__c, appT[0].Admin__c);
            TemplateDataMap__c tdm = new TemplateDataMap__c();
            
            GenerateUtil.populateFromTemplateDataMap(sc, tdm);
            GenerateUtil.testVar = 'Published';
            GenerateUtil.updateBatch(b.Id,'Gforce2 06/26/2008 05:24:09 AM',t.Id,'2008-06-17','2008-06-25','https://www.yahoo.com','test');    
            GenerateUtil.testVar = 'Plan Approved 1';
            GenerateUtil.updateBatch(b.Id,'Gforce2 06/26/2008 05:24:09 AM',t.Id,'2008-06-17','2008-06-25','https://www.yahoo.com','test');    
            GenerateUtil.testVar = 'Plan Rejected 1';
            GenerateUtil.updateBatch(b.Id,'Gforce2 06/26/2008 05:24:09 AM',t.Id,'2008-06-17','2008-06-25','https://www.yahoo.com','test');    
    
            GenerateUtil.updatePlanDocStatus(new String[]{String.valueOf(sc.Id)}, b.Id,'https://www.yahoo.com','2008-06-25');
            GenerateUtil.testVar = 'Published';
            GenerateUtil.updatePlanDocStatus2(new String[]{String.valueOf(sc.Id)}, b.Id,'https://www.yahoo.com','2008-06-25', t.Id);
            GenerateUtil.testVar = 'Plan Approved 1';
            GenerateUtil.updatePlanDocStatus2(new String[]{String.valueOf(sc.Id)}, b.Id,'https://www.yahoo.com','2008-06-25', t.Id);
            GenerateUtil.testVar = 'Plan Rejected 1';
            GenerateUtil.updatePlanDocStatus2(new String[]{String.valueOf(sc.Id)}, b.Id,'https://www.yahoo.com','2008-06-25', t.Id);
            
            Profile[] p = [select id from Profile where name = 'System Administrator'];
            User[] us = [select id from User where ProfileId != :p[0].Id limit 1];
            User u;
            Approvers__c app1;
            if(us.size() == 0) 
            {
                u = new User(Lastname = 'enduser', Username = 'enduser1@gmail.com', Email = 'enduser@gmail.com', Alias = 'enduser', CommunityNickname = 'euser', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'ISO-8859-1', ProfileId = p[0].Id, LanguageLocaleKey = 'en_US', ManagerId = UserInfo.getUserId());
                try
                {
                    insert u;
                }
                catch(Exception e){}
                GenerateUtil.shareRecordToUser(sc.Id, u.Id);
                app1 = new Approvers__c(Level1__c = u.Id, Level2__c = u.Id, Level3__c = u.Id, Level4__c = u.Id, Level5__c = u.Id, Level6__c = u.Id, Level7__c = u.Id, Level8__c = u.Id, Level9__c = u.Id, Level10__c = u.Id, Level11__c = u.Id, Level12__c = u.Id, Level13__c = u.Id, Level14__c = u.Id, Level15__c = u.Id, Level1_Due_Date__c = Date.today(), Level2_Due_Date__c = Date.today(), Level3_Due_Date__c = Date.today(), Level4_Due_Date__c = Date.today(), Level5_Due_Date__c = Date.today(), Level6_Due_Date__c = Date.today(), Level7_Due_Date__c = Date.today(), Level8_Due_Date__c = Date.today(), Level9_Due_Date__c = Date.today(), Level10_Due_Date__c = Date.today(), Level11_Due_Date__c = Date.today(), Level12_Due_Date__c = Date.today(), Level13_Due_Date__c = Date.today(), Level14_Due_Date__c = Date.today(), Level15_Due_Date__c = Date.today(), Final_Approver__c = u.Id, Final_Approver_Date__c = Date.today());
                GenerateUtil.populateSalesContractLevels(sc, app1, String.valueOf(u.Id));
            }
            else
            {
                GenerateUtil.shareRecordToUser(sc.Id, us[0].Id);
                app1 = new Approvers__c(Level1__c = us[0].Id, Level2__c = us[0].Id, Level3__c = us[0].Id, Level4__c = us[0].Id, Level5__c = us[0].Id, Level6__c = us[0].Id, Level7__c = us[0].Id, Level8__c = us[0].Id, Level9__c = us[0].Id, Level10__c = us[0].Id, Level11__c = us[0].Id, Level12__c = us[0].Id, Level13__c = us[0].Id, Level14__c = us[0].Id, Level15__c = us[0].Id, Level1_Due_Date__c = Date.today(), Level2_Due_Date__c = Date.today(), Level3_Due_Date__c = Date.today(), Level4_Due_Date__c = Date.today(), Level5_Due_Date__c = Date.today(), Level6_Due_Date__c = Date.today(), Level7_Due_Date__c = Date.today(), Level8_Due_Date__c = Date.today(), Level9_Due_Date__c = Date.today(), Level10_Due_Date__c = Date.today(), Level11_Due_Date__c = Date.today(), Level12_Due_Date__c = Date.today(), Level13_Due_Date__c = Date.today(), Level14_Due_Date__c = Date.today(), Level15_Due_Date__c = Date.today(), Final_Approver__c = us[0].Id, Final_Approver_Date__c = Date.today());
                GenerateUtil.populateSalesContractLevels(sc, app1, String.valueOf(us[0].Id));
            }
    
            Test.stopTest();
        }
        catch(Exception e){}
    }

    static testMethod void testCase3() 
    {
        try
        {
            Template__c[] tempT = null;
            Batch__c[] batchT = null;
            BatchedUser__c[] buT = null;
            
            Template__c t = new Template__c(name = 'generateTitle3', EligibilityType__c = 'Title');
            
            Test.startTest();
            insert t;
            tempT = [select name from Template__c where id = :t.id];
            system.assertEquals(t.name, tempT[0].name);
            
            Batch__c btch = new Batch__c(Name='generateTest3', Effective_Start_Date__c = Date.today(), Effective_End_Date__c = Date.today(), Status__c = 'Completed', StartTime__c = System.now(), EndTime__c = System.now(), TotalContracts__c = 5, RemainingContracts__c = 3, TemplateId__c = t.Id);
            insert btch;
            batchT = [select name from Batch__c where id = :btch.id];
            system.assertEquals(btch.name, batchT[0].name);
            
            BatchedUser__c bu = new BatchedUser__c(Batchid__c = btch.Id);
            insert bu;
            buT = [select Batchid__c from BatchedUser__c where id = :bu.id];
            system.assertEquals(bu.Batchid__c, buT[0].Batchid__c);
            
            Approvers__c app = new Approvers__c(Template__c = t.Id, Admin__c = UserInfo.getUserId());
            insert app;
            Approvers__c[] appT = [select Template__c, Admin__c from Approvers__c where id=:app.id];
            system.assertEquals(app.Template__c, appT[0].Template__c);
            system.assertEquals(app.Admin__c, appT[0].Admin__c);
            Template_Role__c tr = new Template_Role__c(Template_ID__c = t.Id, Template__c = t.Id, User_Title__c = 'Account Executive East');//Role_Id__c = '00E40000000ju5r');
            insert tr;
            Template_Role__c[] trT = [select Template_ID__c  from Template_Role__c where id = :tr.id];
            system.assertEquals(tr.Template_ID__c, trT[0].Template_ID__c);

            Attachment att = new Attachment(name = 'generateAttach', body = Blob.valueOf('<html><body>This is example for {TemplateAttachment}.</body></html>'), parentId = t.Id);
            insert att;
            Attachment[] attT = [select name from Attachment where id = :att.id];
            system.assertEquals(att.name, attT[0].name);
            System.assertEquals(GenerateUtil.generateContract(t.Id, '2008-06-26', 'Local'), 'success');
        
            String[] attachList = new String[]{att.id, att.name};
            
            System.assertEquals(GenerateUtil.getTemplateAttachments(t.Id), attachList);
            
            GenerateUtil.getCurrentDateTime('01:49:44 AM');
            
            
            Test.stopTest();
        }
        catch(Exception e){}
        
    }

    static testMethod void testCase4() 
    {

        Template__c[] tempT = null;
        Template__c t = new Template__c(name = 'generateTitle', EligibilityType__c = 'Title');
        Test.startTest();
        GenerateUtil.testVar = 'test';
        GenerateUtil.generateContract2(t.Id, '2008-06-26', 'Local', null, '2008-06-25', '2008-06-25');
        Test.stopTest();
    }   

    static testMethod void testCase5() {
            Template__c[] tempT = null;
            Batch__c[] batchT = null;
            BatchedUser__c[] buT = null;
            
            Template__c t = new Template__c(name = 'generateTitle3', EligibilityType__c = 'Role');
            
            Test.startTest();
            insert t;
            tempT = [select name from Template__c where id = :t.id];
            system.assertEquals(t.name, tempT[0].name);
            
            Batch__c btch = new Batch__c(Name='generateTest3', Effective_Start_Date__c = Date.today(), Effective_End_Date__c = Date.today(), Status__c = 'Completed', StartTime__c = System.now(), EndTime__c = System.now(), TotalContracts__c = 5, RemainingContracts__c = 3, TemplateId__c = t.Id);
            insert btch;
            batchT = [select name from Batch__c where id = :btch.id];
            system.assertEquals(btch.name, batchT[0].name);
            
            BatchedUser__c bu = new BatchedUser__c(Batchid__c = btch.Id);
            insert bu;
            buT = [select Batchid__c from BatchedUser__c where id = :bu.id];
            system.assertEquals(bu.Batchid__c, buT[0].Batchid__c);
            
            Approvers__c app = new Approvers__c(Template__c = t.Id, Admin__c = UserInfo.getUserId());
            insert app;
            Approvers__c[] appT = [select Template__c, Admin__c from Approvers__c where id=:app.id];
            system.assertEquals(app.Template__c, appT[0].Template__c);
            system.assertEquals(app.Admin__c, appT[0].Admin__c);
            Template_Role__c tr = new Template_Role__c(Template_ID__c = t.Id, Template__c = t.Id, Role_Name__c = 'Account Executive', Role_Id__c = '00E40000000ju5r');
            insert tr;
            Template_Role__c[] trT = [select Template_ID__c  from Template_Role__c where id = :tr.id];
            system.assertEquals(tr.Template_ID__c, trT[0].Template_ID__c);

            Attachment att = new Attachment(name = 'generateAttach', body = Blob.valueOf('<html><body>This is example for {TemplateAttachment}.</body></html>'), parentId = t.Id);
            insert att;
            Attachment[] attT = [select name from Attachment where id = :att.id];
            system.assertEquals(att.name, attT[0].name);
            System.assertEquals(GenerateUtil.generateContract(t.Id, '2008-06-26', 'Local'), 'success');
        
            String[] attachList = new String[]{att.id, att.name};
            
            System.assertEquals(GenerateUtil.getTemplateAttachments(t.Id), attachList);
            
            GenerateUtil.getCurrentDateTime('01:49:44 AM');
            
            GenerateUtil.generateContract2(t.Id, '2008-06-26', 'Local', att.Id, '2008-06-25', '2008-06-25');
            
            Test.stopTest();
        
    }
    
}